﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using Wicresoft.SMS.Adapter.Interface;
using System.Reflection;

namespace Wicresoft.SMS.Debuger
{
    public partial class Main : Form
    {
        private Sites sites = null;
        private ISMSAdapter adapter = null;
        private SitesSite curSite = null;

        public Main()
        {
            InitializeComponent();
            LoadConfig();
            if (sites == null) return;
            if (sites.Site == null) return;
            foreach (var s in sites.Site)
            {
                cbm_Site.Items.Add(s.display);
            }

            if (sites.remember == 0) return;
            this.txt_Mobile.Text = sites.remember.ToString();
            this.chk_remember.Checked = true;
        }

        private void LoadConfig()
        {
            if (!File.Exists("Config.xml"))
            {
                Logger.FormDebug(this.txt_Debuger, "Missing Config.xml.");
            }
            string configStr = File.ReadAllText("Config.xml", Encoding.UTF8);
            try
            {
                XmlSerializer mySerializer = new XmlSerializer(typeof(Sites));
                StreamReader mem2 = new StreamReader(
                        new MemoryStream(System.Text.Encoding.Unicode.GetBytes(configStr)),
                        System.Text.Encoding.Unicode);
                sites = (Sites)mySerializer.Deserialize(mem2);
            }
            catch(Exception ex)
            {
                Logger.FormDebug(this.txt_Debuger, "Deserialize Error.\n" + ex.Message);
            }

 
        }

        private ISMSAdapter LoadAdapter(string site)
        {
            ISMSAdapter retAdapter = null;
            if (sites == null) throw new Exception("Missing Sites Config.");
            if (sites.Site == null) throw new Exception("No Sites Config found.");

            curSite = sites.Site.FirstOrDefault(s => s.display == site);
            if (curSite == null) throw new Exception("Site of " + site + " not found.");
            if (curSite.Dependency == null) throw new Exception("Site of " + site + "'s dependency is not found.");
            if (curSite.Dependency.Assemblies == null) throw new Exception("Site of " + site + "'s dependency's assemblies are not found.");

            foreach (var ass in curSite.Dependency.Assemblies)
            {
                if (!File.Exists(ass.name)){
                    throw new Exception("Site of " + site + "'s dependency's assembly : " + ass.name + " is not found.");
                }
                if (ass.@class != null && ass.@class != string.Empty)
                {
                    if (retAdapter == null)
                    {
                        Assembly assembly = Assembly.LoadFrom(ass.name);
                        retAdapter = assembly.CreateInstance(ass.@class) as ISMSAdapter;
                    }
                    else
                    {
                        throw new Exception("Site of " + site + " have duplicate dependency assembly have 'class'");
                    }
 
                }
            }

            return retAdapter;
        }

        private void cbm_Site_SelectedIndexChanged(object sender, EventArgs e)
        {
            //reset curSite and adapter
            curSite = null;
            if(adapter != null)
            {
                adapter.Dispose();
                adapter = null;
            }

            //try to load site and adapter
            try
            {
                adapter = LoadAdapter(cbm_Site.SelectedItem.ToString());
                if (adapter == null || curSite == null)
                {
                    Logger.FormDebug(txt_Debuger, "Error load adapter.");
                }
                else
                {
                    Logger.FormDebug(txt_Debuger, adapter.Init(curSite.Dependency.Configuration.Connection.str));
                }

            }
            catch (Exception ex)
            {
                Logger.FormDebug(txt_Debuger, ex.ToString());
            }

        }

        private void btn_Send_Click(object sender, EventArgs e)
        {
            if (adapter == null)
                Logger.FormDebug(txt_Debuger, "No adapter init.");
            else
            {
                if (txt_Mobile.Text != string.Empty && txt_Content.Text != string.Empty)
                {
                    try
                    {
                        Logger.FormDebug(txt_Debuger, adapter.SendInTimeSMS(new string[] { txt_Mobile.Text }, txt_Content.Text));
                    }
                    catch(Exception ex)
                    {
                        Logger.FormDebug(txt_Debuger, ex.ToString());
                    }
                }
            }
        }

        private void btn_Copy_Click(object sender, EventArgs e)
        {
            if(txt_Debuger.Text != string.Empty)
                Clipboard.SetText(txt_Debuger.Text);
        }
    }
}
